﻿///Reset the Frame size.

///<summary>
//判断中文的日期
//<returns>true/false</returns>
///<summary>
function validateCNDate( strValue )
{
	strValue = strValue.split(" ")[0];
	var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
	if(!objRegExp.test(strValue))
	{
		return false;
	}
	else
	{
		var arrayDate = strValue.split(RegExp.$1); 
		var intDay = parseInt(arrayDate[2],10);
		var intYear = parseInt(arrayDate[0],10);
		var intMonth = parseInt(arrayDate[1],10);

		if(intMonth > 12 || intMonth < 1) 
		{
			return false;
		}
		var arrayLookup = { '1' : 31,'3' : 31, '4' : 30,'5' : 31,'6' : 30,'7' : 31,'8' : 31,'9' : 30,'10' : 31,'11' : 30,'12' : 31}
		if (arrayLookup[parseInt(intMonth)] != null) 
		{
			if(intDay <= arrayLookup[parseInt(intMonth)] && intDay != 0)
			{
				return true; 
			}
		}
		if (intMonth-2 ==0) 
		{
			var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
			if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
			{
				return true;
			}
		}
	}
	return false; 
}

function CheckReadonly(TargetBoxID, enable)
{
    var txt = document.getElementById(TargetBoxID);
    if (enable)
    {
        txt.readOnly = false;
        txt.tabIndex = "";
        txt.className = "";
    }
    else
    {
        txt.readOnly = true;
        txt.tabIndex = "10000000";
        txt.className = "ReadonlyText";
    }
}
function OpenDialog(url, data, height, width, scroll, resizable)
{
	var returndialog = window.showModalDialog(url, data, "dialogHeight:" + height + "px;dialogWidth:" + width + "px;center:yes;resizable:" + resizable + ";status:no;help:no;scroll:" + scroll);
	if (returndialog == "AuthTimeOut")
	{
	    self.location = self.location;
	}
	else
	{
	    return returndialog;
	}
}

///<summary>
//设置文本输入框中的日期按钮
///<summary>
function SetPickDateButton(TargetBox)
{
    if (TargetBox.SetButton != "true")
    {
        TargetBox.parentElement.noWrap = true;
        var btn = document.createElement("img");
        btn.id = TargetBox.id + "_ImageButton";
        btn.src = "../Images/PickDateButton.gif";
        btn.attachEvent("onclick", new Function("ClientPickDate('" + TargetBox.id + "');"));
        btn.className = "InputImage";
        btn.align = "absmiddle";
        TargetBox.insertAdjacentElement("AfterEnd", btn);
        TargetBox.SetButton = "true";
    }
}

///<summary>
//设置文本输入框中的日期按钮
///<summary>
function SetPickDateButtonText(TargetBox)
{     
    
        ClientPickDate(TargetBox)
        
   }



///<summary>
//弹出日期选择窗口设置被选中的日期
///<summary>
function ClientPickDate(TargetBoxID)
{      
	var selDate = OpenDialog("../Search/PopCalendar.aspx", "", 240, 260, 'yes', 'yes');
	if ((selDate != null) && (selDate != "cancel") && (selDate != "undefined"))
	{	    	    
	   
		document.getElementById(TargetBoxID).value = selDate;
	}
}

///<summary>
//设置文本输入框中的查找按钮
///<summary>
function SetPickCustomerButton(TargetBox)
{
    if (TargetBox.SetButton != "true")
    {
        TargetBox.parentElement.noWrap = true;
        var btn = document.createElement("img");
        btn.id = TargetBox.id + "_ImageButton";
        btn.src = "/Image/Find.gif";
        btn.attachEvent("onclick", new Function("ClientPickCustomer('" + TargetBox.id + "');"));
        btn.className = "InputImage";
        btn.align = "absmiddle";
        TargetBox.insertAdjacentElement("AfterEnd", btn);
        TargetBox.SetButton = "true";
        TargetBox.parentElement.style.width = TargetBox.parentElement.style.width;
    }
}

///<summary>
//弹出日期选择窗口设置被选中的日期
///<summary>
function ClientPickCustomer(TargetBoxID)
{
    var showAll = (document.getElementById(TargetBoxID).ShowAll == "True");
    var url = "GetFile.aspx?type=resource&path=/SubSystem/Solaris/DataEntry.aspx?entrycode=CUSTOMER_LIST";
    var title = "Customer List";
    if (showAll)
    {
        url = "GetFile.aspx?type=resource&path=/SubSystem/Solaris/DataEntry.aspx?entrycode=CUSTOMER_LIST&show=p";
        title = "Producer List";
    }
	selDate = OpenFrameDialog(url, title, 400, 600, 'yes', 'yes');
	if ((selDate != null) && (selDate != "cancel") && (selDate != "undefined"))
	{
		document.getElementById(TargetBoxID).value = selDate.split("^")[1];
		document.getElementById(TargetBoxID + "_id").value = selDate.split("^")[0];
	}
}

///<summary>
//若文本框的类型为text则选中文本框的值，否则为password时则清空文本框
///<summary>
function GetBack(ElementID)
{
	var TargetElement = document.getElementById(ElementID);
	TargetElement.focus();
	if (TargetElement.type == "text")
	{
		TargetElement.select();
	}
	else if (TargetElement.type == "password")
	{
		TargetElement.value = "";
	}
}

///<summary>
//判断文本框的内容是否为空，若为空的话弹出报错信息
// <param name="ElementID">文本框的ID</param>
// <param name="WarningMessage">错误消息</param>
//<returns>true/false</returns>
///<summary>
function IsNull(ElementID, WarningMessage)
{
	TargetElement = document.getElementById(ElementID);
	if(TargetElement.value.length == 0)
	{
		alert(WarningMessage);
		GetBack(ElementID);
		return true;
	}
	return false;
}

///<summary>
//判断文本框的内容是否与传进来的字符串相等，如果相等则报出提示信息
// <param name="ElementID">文本框的ID</param>
// <param name="MatchString">与文本框的内容进行比较的字符串</param>
// <param name="WarningMessage">弹出提示信息的内容</param>
//<returns>true/false</returns>
///<summary>
function IsMatchString(ElementID, MatchString, WarningMessage)
{
	TargetElement = document.getElementById(ElementID);
	if(TargetElement.value == MatchString)
	{
		alert(WarningMessage);
		GetBack(ElementID);
		return true;
	}
	return false;
}

function FormatNumber(elementID, digits)
{
	var targetElement = document.getElementById(elementID);
    var s = targetElement.value;
    var originLength = s.length;
    var isMinus = false;
    if (s != "")
    {    
        if (s.substr(0, 1) == "-")
        {
            isMinus = true;
            s = s.substr(1);
        }
        s = parseFloat(s);
        
        // 加0.5代表四舍五入
        s = parseInt(s * Math.pow(10, digits) + 0.5).toString();
    		        
        if (s.length < digits)
        {
            s = "000000000000" + s;
            s = s.substr(s.length - digits - 1, digits + 1);
        }
        var dotPos = s.length - digits;
        s = s.substr(0, dotPos) + "," + s.substr(dotPos, digits);
        while (s.indexOf(",") > 3)
        {
            var commaPos = s.indexOf(",") - 3;
            s = s.substr(0, commaPos) + "," + s.substr(commaPos, s.length - commaPos);
        }
        if (digits > 0)
        {
            dotPos = s.lastIndexOf(",");
            s = s.substr(0, dotPos) + "." + s.substr(dotPos + 1, digits);
        }
        else
        {
            s = s.substr(0, s.length - 1);
        }
        if (isMinus)
        {
            s = "-" + s;
        }
        targetElement.value = s;
    }
}

///<summary>
//设置数字文本框的值
// <param name="elementID">数字文本框的ID</param>
// <param name="message">错误的提示信息</param>
// <param name="digits">填写的小数位数</param>
///<summary>
function NumberValidate(elementID, message, digits, toElement)
{
    if ((event.toElement != null) && (event.toElement.id != "ButClear") && (event.toElement.id != "ButCancel"))
    {
	    var targetElement = document.getElementById(elementID);
        var s = targetElement.value;
        var desc = targetElement.FieldDesc;
        var min = targetElement.MinValue;
        var max = targetElement.MaxValue;
        var invalidNumber = false;
        var invalidLessValue = false;
        var invalidMoreValue = false;
        var isMinus = false;
        if (s != "")
        {
            s = s.replace(/,/g, "");
		    if (isNaN(s))
		    {
			    invalidNumber = true;
		    }
		    else
		    {
		        var originLength = s.length;
		        
		        // check whether the value less than the min value;
		        if ((min != "") && (s < parseFloat(min)))
		        {
		            invalidLessValue = true;
		        }
		        
		        if (s.substr(0, 1) == "-")
		        {
		            isMinus = true;
		            s = s.substr(1);
		        }
		        s = parseFloat(s);
		        if (s > 999999999999)
		        {
    			    invalidNumber = true;
		        }
		        
		        // check whether the value more than the max value;
		        if ((max != 0) && (s > parseFloat(max)))
		        {
		            invalidMoreValue = true;
		        }
		        
		        // 加0.5代表四舍五入
		        s = parseInt(s * Math.pow(10, digits) + 0.5).toString();
		        
		        if ((originLength > 10) && (s.length == 1))
		        {
    			    invalidNumber = true;
		        }
		        
		        if (s.length < digits)
		        {
		            s = "000000000000" + s;
		            s = s.substr(s.length - digits - 1, digits + 1);
		        }
		        
	            var dotPos = s.length - digits;
	            s = s.substr(0, dotPos) + "," + s.substr(dotPos, digits);
		        while (s.indexOf(",") > 3)
		        {
    		        var commaPos = s.indexOf(",") - 3;
		            s = s.substr(0, commaPos) + "," + s.substr(commaPos, s.length - commaPos);
		        }
		        if (digits > 0)
		        {
                    dotPos = s.lastIndexOf(",");
                    s = s.substr(0, dotPos) + "." + s.substr(dotPos + 1, digits);
		        }
		        else
		        {
		            s = s.substr(0, s.length - 1);
		        }
		        if (isMinus)
		        {
		            s = "-" + s;
		        }
		    }
        }
        if (invalidNumber)
        {
		    GetBack(elementID);
		    alert(message);
		    event.returnValue=false;
        }
        else
        {
            if (invalidLessValue)
            {
		        GetBack(elementID);
		        alert("Value of [" + desc + "] cannot less than '" + min + "'. \r\nplease check and input again.");
		        event.returnValue=false;
            }
            else if (invalidMoreValue)
            {
		        GetBack(elementID);
		        alert("Value of [" + desc + "] cannot more than '" + max + "'. \r\nplease check and input again.");
		        event.returnValue=false;
            }
            else
            {
                targetElement.value = s;
            }
        }
    }
} 

///<summary>
//判断在单击日期选择框时在点击未选中图片时报出的信息
// <param name="message">提示信息</param>
///<summary>
function DateValidate(message)
{
    if ((event.toElement != null) && (event.toElement.id != "ButClear"))
	{
	    if ((event.offsetX < 0) || (event.offsetX > event.srcElement.offsetWidth - 3) || (event.offsetY < 0) || (event.offsetY > event.srcElement.offsetHeight - 3))
	    {
		    if ((event.srcElement.value != "") && (!validateCNDate(event.srcElement.value)))
		    {
			    GetBack(event.srcElement.id);
			    alert(message);
			    event.returnValue=false;
			  
		    }
	    }
	}
}

///<summary>
// Email的格式判断
// <param name="message">提示信息</param>
///<summary>
function ValidateEmail(Message)
{
	var email = event.srcElement.value;
	var valid = /^[_.a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/.test(email);
	if ((email != "") && (!valid))
	{
		GetBack(event.srcElement.id);
		ClientAlert(Message);
		event.returnValue=false;
	}
}

///<summary>
// 设置复选框及单选框的大小
// <param name="message">提示信息</param>
///<summary>
function SetInputWidth()
{
    var checkradio = document.all.tags("input");
    for (var i = 0; i < checkradio.length; i++)
    {
        if ((checkradio[i].type == "checkbox") || (checkradio[i].type == "radio"))
        {
            checkradio[i].style.width = "30px";
        }
    }
}

self.attachEvent("onload", SetInputWidth);


///<summary>
//判断文本框的Id是否为空
// <param name="message">提示消息</param>
// <param name="fieldID">控件Id</param>
//<returns>true/false</returns>
///<summary>
function FieldCheckEmpty(message, fieldID)
{
    var f = document.getElementById(fieldID);
    if ((f.name != "") && (f.value == ""))
    {
        GetBack(f.id);
        alert(message.replace("@@", f.FieldDesc));
        return false;
    }
    else
    {
        return true;
    }
}

// check all cant-be-empty textbox on page
function FormCheckEmpty(message, objectID)
{
    var input, select, textarea;
    
    if (objectID == null)
        input = document.forms[0].all.tags("input");
    else
        input = document.getElementById(objectID).all.tags("input");
    for (i = 0; i< input.length; i++)
    {
        var f = input[i];
        if ((f.type != "checkbox") && (f.CheckEmpty != null) && (f.CheckEmpty.toLowerCase() == "true") && (!FieldCheckEmpty(message, f.id)))
        {
            return false;
        }
    }
    
    if (objectID == null)
        select = document.forms[0].all.tags("select");//alert(select);
    else
        select = document.getElementById(objectID).all.tags("select");
    for (i = 0; i < select.length; i++)
    {
        var sel = select[i];
        if ((sel.CheckEmpty != null) && (sel.CheckEmpty.toLowerCase() == "true") && (!FieldCheckEmpty(message, sel.id)))
        {
            return false;
        }
    }
    
    if (objectID == null)
        textarea = document.forms[0].all.tags("textarea");
    else
        textarea = document.getElementById(objectID).all.tags("textarea");
    for (i = 0; i < textarea.length; i++)
    {
        var txt = textarea[i];
        if ((txt.CheckEmpty != null) && (txt.CheckEmpty.toLowerCase() == "true") && (!FieldCheckEmpty(message, txt.id)))
        {
            return false;
        }
    }
    return true;
}

///<summary>
//判断控件Id的值相加是否等于100
// <param name="objectID">控件ID</param>
// <param name="message">提示消息</param>
//<returns>true/false</returns>
///<summary>
function Check100Percentage(objectID, message)
{
    if (document.getElementById(objectID).style.display != "none")
    {
        var txts = document.getElementById(objectID).all.tags('input');
        var values = 0;
        var desc = document.getElementById(objectID).Description;
        for (var i = 0; i < txts.length; i++)
        {
            if ((txts[i].type == 'text') && (!txts[i].readOnly))
            {
                if ( txts[i].value.replace(/,/g, '') != '')
                {
                    var value = parseFloat(txts[i].value.replace(/,/g, '')) * 100;
                    if (!isNaN(value))
                    {
                        values = values + value;
                    }
                }
            }
        }
        if (values == 10000)
        {
            return true;
        }
        else
        {
            alert(message.replace('@@', desc));
            return false;
        }
    }
    else
    {
        return true;
    }
}

function Check100Percentages(objectIDs, messages)
{
    var allValid = true;
    if (objectIDs != "")
    {
        var objectIDcol = objectIDs.split('^');
        var messagecol = messages.split('^');
        for (var i = 0; i < objectIDcol.length; i++)
        {
            var objectID = objectIDcol[i];
            var message = messagecol[i];
            if (!Check100Percentage(objectID, message))
            {
                allValid = false;
                break;
            }
        }
    }
    return allValid;
}

///<summary>
//判断是否结束时间大于开始时间
// <param name="startDate">开始时间</param>
// <param name="endDate">结束时间</param>
//<returns>true/false</returns>
///<summary>
function DateCompare(startDate, endDate)
{
    var sy = startDate.split("-")[0];
    var sm = "00" + startDate.split("-")[1];
    sm = sm.substr(sm.length - 2);
    var sd = "00" + startDate.split("-")[2];
    sd = sd.substr(sd.length - 2);
    
    var ey = endDate.split("-")[0];
    var em = "00" + endDate.split("-")[1];
    em = em.substr(em.length - 2);
    var ed = "00" + endDate.split("-")[2];
    ed = ed.substr(ed.length - 2);
    
    var s = parseInt(sy + sm + sd);
    var e = parseInt(ey + em + ed);
    
    return (s <= e);
}